package datastructure.book.dp._2_linear_pre_section.single.all;

public class _9_Solution {

    public int rob(int[] nums) {
        int[] dp = new int[nums.length];
        int max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            int tempMax = 0;
            for (int j = 0; j < i-1; j++) {
                tempMax = Math.max(tempMax,dp[j]);
            }
            dp[i] = tempMax+nums[i];
            max = Math.max(max,dp[i]);
        }
        return max;
    }
    public int rob2(int[] nums) {
        if (nums.length < 2) {
            return nums[0];
        }
        int max = Math.max(nums[0],nums[1]);
        if (nums.length == 2) {
            return max;
        }
        if (nums.length < 4) {
            return Math.max(max,nums[2]+nums[0]);
        }
        int[] dp = new int[nums.length];
        dp[0] = nums[0];dp[1] = nums[1];dp[2] = nums[0]+nums[2];
        for (int i = 3; i < nums.length; i++) {
            dp[i] = Math.max(dp[i-2],dp[i-3])+nums[i];
        }
        return Math.max(dp[dp.length-1],dp[dp.length-2]);
    }
}
